package com.example.myapplication2048.slice;

import com.example.myapplication2048.ResourceTable;
import com.example.myapplication2048.datamodel.*;
import com.example.myapplication2048.itemprovider.MainBodyContentProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Rect;
import ohos.agp.window.service.Window;
import ohos.bundle.ApplicationInfo;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    //初始化按钮数量
    private static final int BOTTOM_TAB_BUTTON_NUM =5;
    private List<DirectionalLayout> layouts = new ArrayList<>();
    private PageSlider MainBodyPageSlider;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
       initTabs();//设置底部tabbar
        initBottom();
        initmainBodyContent();
    }
    private void initmainBodyContent(){
         MainBodyPageSlider =(PageSlider) findComponentById(ResourceTable.Id_main_content_page_slider);
        List<DirectionalLayout> directionalLayouts = getlistMainBodyContent();
        MainBodyContentProvider mainBodyContentProvider = new MainBodyContentProvider(directionalLayouts);
        MainBodyPageSlider.setProvider(mainBodyContentProvider);
//        MainBodyPageSlider.setSlidingPossible(false);//关闭滑动
    }
    public List<DirectionalLayout>  getlistMainBodyContent(){
        DirectionalLayout Layout_system = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_system, null, false);
        DirectionalLayout Layout_api = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_api, null, false);
        DirectionalLayout Layout_layoutCompontents = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_layoutCompontents, null, false);
        DirectionalLayout Layout_data_transfer = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_data_transfer, null, false);
        DirectionalLayout Layout_net_transfer = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_net_transfer, null, false);
        layouts.add(Layout_layoutCompontents);
        layouts.add(Layout_system);
        layouts.add(Layout_api);
        layouts.add(Layout_data_transfer);
        layouts.add(Layout_net_transfer);
        return layouts;

}
    private void setDynamicProvider(){
//        获取渲染的列表根组件
        /**
         *         <ListContainer
         *             ohos:id="$+id:list_container2"
         *             ohos:height="match_content"
         *             ohos:width="match_parent"
         *             />
         *               <ListContainer
         *         ohos:id="$+id:list_container"
         *         ohos:height="match_content"
         *         ohos:width="match_parent"
         *         />
         */
//        ListContainer listContainerDynamic =(ListContainer)  findComponentById(ResourceTable.Id_list_container);
//        DynamicViewModel viewModel = new DynamicViewModel();
//        List<DynamicItemInfo> dataItemList = viewModel.getDataItemList();
//        System.out.println("llt"+dataItemList.size());
//        DynamicProvider dynamicProvider = new DynamicProvider(dataItemList);
//        listContainerDynamic.setItemProvider(dynamicProvider);
//
//        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container2);
//       TextViewModel textViewModel = new TextViewModel();
//        List<Textinfo> dataItemList2 = textViewModel.getDataItemList();
//       TextProvider textProvider = new TextProvider(dataItemList2);
//        listContainer.setItemProvider(textProvider);

    }
    private void  initBottom(){
        ArrayList<Textinfo> textinfos = new ArrayList<>();
        textinfos.add(new Textinfo("布局组件"));
        textinfos.add(new Textinfo("系统环境"));
        textinfos.add(new Textinfo("API"));
        textinfos.add(new Textinfo("数据传输"));
        textinfos.add(new Textinfo("网络通信"));
        PageSlider pageSlider = new PageSlider(getContext());
        //获取主页面底部容器ID
        DirectionalLayout bottomTab = (DirectionalLayout) findComponentById(ResourceTable.Id_bottom_tabMenu);
        //获取body内容容器id
//
        //创建底部容器数据数组
        List<DirectionalLayout> tabList = new ArrayList<>();
        for (int count = 0; count < BOTTOM_TAB_BUTTON_NUM; count++) {
            //获取模板容器
            DirectionalLayout tab = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                    .parse(ResourceTable.Layout_bottom_nav_tab, bottomTab, false);
            //获取模板容器组件
            Image buttonImage = (Image) tab.findComponentById(ResourceTable.Id_bottom_tab_button_img);
            Text buttonText = (Text) tab.findComponentById(ResourceTable.Id_bottom_tab_button_text);
            if (buttonImage != null) {//
                if (count == 0) {
                    buttonImage.setPixelMap(ResourceTable.Media_icon_actived);
                } else {
                    buttonImage.setPixelMap(ResourceTable.Media_icon_normal);
                }
            }
            if (buttonText != null) {
                buttonText.setText(textinfos.get(count).getAuthor());
            }
            //绑定点击事件
            bottomNavBarClick(tab,tabList,count);
            tabList.add(tab);
            bottomTab.addComponent(tab);

        }

    }
    /**
     * 设置tab start
     */
    private void initTabs(){
        TabList tabs = (TabList) findComponentById(ResourceTable.Id_tab_list);
        TabList.Tab tab = tabs.new Tab(getContext());
        tab.setText("关注");
        TabList.Tab tab2 = tabs.new Tab(getContext());
        tab2.setText("推荐");
        tabs.addTab(tab,true);
        tabs.addTab(tab2);
    }

    /**
     * 自定义底部点击事件
     * @param item   当前组件
     * @param tabList  底部组件列表
     * @param index  当前索引值
     */
    private void bottomNavBarClick(DirectionalLayout item,List<DirectionalLayout> tabList,int index){

        item.setClickedListener(component -> {
            // Deselect all tabs in tab menu
            int i=0;
            for (DirectionalLayout btn : tabList) {
                Image componentById = (Image) btn.findComponentById(ResourceTable.Id_bottom_tab_button_img);
                componentById.setPixelMap(ResourceTable.Media_icon_normal);
                i++;
            }
            Image componentById =(Image) component.findComponentById(ResourceTable.Id_bottom_tab_button_img);
            componentById.setPixelMap(ResourceTable.Media_icon_actived);
            MainBodyPageSlider.setCurrentPage(index);
        });

    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
